package com.wtwd.campus.receiver;

import com.alibaba.fastjson.JSONObject;
import com.wtwd.campus.annotation.MsgType;
import com.wtwd.campus.common.Constants;
import com.wtwd.campus.entity.DeviceInfo;
import com.wtwd.campus.entity.MqMessage;
import com.wtwd.campus.entity.MqStepAndBatteryMessage;
import com.wtwd.campus.h5_manager.entity.DeviceLog;
import com.wtwd.campus.h5_manager.service.DeviceLogService;
import com.wtwd.campus.service.DeviceInfoService;
import com.wtwd.campus.service.WarningRecordService;
import com.wtwd.campus.utils.ReceiverUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@MsgType(value = Constants.GET_DEVICE_STEP)
public class ActiveGetStepReceiverHandler implements IReceiverHandler {
    @Autowired
    private DeviceInfoService deviceInfoService;
    @Autowired
    private WarningRecordService warningRecordService;
    @Autowired
    private ReceiverUtils receiverUtils;
    @Autowired
    private DeviceLogService deviceLogService;
    // 低电界限
    private int lowBatteryValue = 20;

    @Override
    public void handlerMessage(MqMessage mqMessage) {
        MqStepAndBatteryMessage mqStepAndBatteryMessage = (MqStepAndBatteryMessage) mqMessage.getObject();
        String imei = mqStepAndBatteryMessage.getImei();
        String commandNumber = mqStepAndBatteryMessage.getCommandNumber();
        Integer stepNum = mqStepAndBatteryMessage.getStepNum();
        Integer battery = mqStepAndBatteryMessage.getBattery();
        //处理指令上报的时间
        String date = mqStepAndBatteryMessage.getDeviceDate();
        String time1 = mqStepAndBatteryMessage.getDeviceTime();
        Long time = getTime(date, time1);
        DeviceInfo deviceInfo = deviceInfoService.getDeviceByImei(imei);
        if (deviceInfo != null) {
            //修改设备信息
            deviceInfo.setLastUpTime((new Date()).getTime());
            deviceInfo.setBattery(battery);
            deviceInfoService.modifyDeviceInfo(deviceInfo);
            /*  现在不需要显示低电电量，而且，如果多次的话，会导致大量低电数据重复
            //判断是否进行低电告警
            if (battery < lowBatteryValue) {
                //存储低电告警记录
                WarningRecord record = new WarningRecord();
                record.setDevice_id(deviceInfo.getDeviceId());
                record.setWarning_type(Constants.LOW_BATTERY_WARNING_TYPE);
                record.setWarning_time(time);
                record.setAddress(deviceInfo.getAddress());
                warningRecordService.addWarningRecord(record);
            }*/
            //删除缓存中的指令
            String delKey = Constants.GET_DEVICE_STEP.concat("-").concat(commandNumber);
            receiverUtils.deleteCommandInRedis(imei, delKey);
            //完善指令下发记录
            receiverUtils.completeCommandRecord(Constants.GET_DEVICE_STEP, imei, commandNumber, null, Constants.COMMAND_COMPLETE, time);
            //存储设备接收日志
            DeviceLog deviceLog = new DeviceLog();
            deviceLog.setSendUser(Constants.DEVICE_USER);
            deviceLog.setCommandNumber(commandNumber);
            deviceLog.setImei(imei);
            deviceLog.setBigType(Constants.MESS_TYPE);
            deviceLog.setType(Constants.GET_DEVICE_STEP);
            JSONObject test = new JSONObject();
            test.put("设备号", imei);
            test.put("步数", stepNum);
            test.put("电量", battery);
            test.put("日月年", date);
            test.put("设备状态", mqStepAndBatteryMessage.getDeviceStatus());
            deviceLog.setCmd(test.toJSONString());
            deviceLog.setSendTime(System.currentTimeMillis());
            deviceLog.setState(1);
            deviceLogService.addDeviceLog(deviceLog);
        } else {
            saveUnInStorage(imei);
            receiverUtils.cloaseChannel(imei);
        }
    }
}
